package com.hzw.saas.web.app.flow.controller;

import java.util.List;

import javax.validation.Valid;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hzw.saas.common.config.annotation.SysLog;
import com.hzw.saas.common.config.util.PageParam;
import com.hzw.saas.common.flow.pojo.bo.FlowInstanceBO;
import com.hzw.saas.common.flow.pojo.bo.FlowProcessRecordBO;
import com.hzw.saas.common.flow.pojo.model.FlowInstance;
import com.hzw.saas.common.flow.service.IFlowInstanceService;
import com.hzw.saas.common.flow.service.IFlowProcessRecordService;
import com.hzw.saas.common.security.utils.SecurityUtils;
import com.hzw.saas.web.app.flow.dto.DoProcessDTO;
import com.hzw.saas.web.app.flow.dto.FlowInstanceCreateDTO;
import com.hzw.saas.web.app.flow.param.DoProcessParam;
import com.hzw.saas.web.app.flow.param.FlowInstanceCreateParam;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zzl
 * @since 08/10/2021
 */
@Controller
@Validated
@RequiredArgsConstructor
@Slf4j
@Api(tags = "流程管理")
public class WorkFlowController {

    private final IFlowInstanceService flowInstanceService;
    private final IFlowProcessRecordService flowProcessRecordService;


    @GetMapping("/flow/instances")
    @SysLog
    @ApiOperation(value = "获取用户发起的流程列表", notes = "获取用户发起的流程列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "flowId", value = "流程定义ID", paramType = "query", required = false)
    })
    @ApiOperationSort(11)
    public ResponseEntity<IPage<FlowInstanceBO>> listUserFlow(PageParam pageParam, String flowId) {
        String userId = SecurityUtils.getUser().getUserId();
        IPage<FlowInstanceBO> result = flowInstanceService.pageFlowInstance(pageParam, userId, flowId);
        return ResponseEntity.ok(result);
    }

    @GetMapping("/flow/instance/{flowInstanceId}/records")
    @SysLog
    @ApiOperation(value = "获取流程处理记录列表", notes = "获取流程处理记录")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "flowInstanceId", value = "流程实例ID", paramType = "path", required = false)
    })
    @ApiOperationSort(12)
    public ResponseEntity<List<FlowProcessRecordBO>> listFlowProcessRecords(@PathVariable String flowInstanceId) {
        List<FlowProcessRecordBO> result = flowProcessRecordService.listRecordBOs(flowInstanceId);
        return ResponseEntity.ok(result);
    }

    // @PostMapping("/flow/instance")
    // @SysLog(operation = "流程发起")
    // @ApiOperation(value = "流程发起", notes = "流程发起")
    // @ApiOperationSort(13)
    public ResponseEntity<FlowInstanceCreateDTO> createFlowInstance(@Validated @RequestBody FlowInstanceCreateParam param) {
        String userId = SecurityUtils.getUser().getUserId();
        FlowInstance flowInstance = flowInstanceService.createFlowInstance(userId, param.getFlowId(), param.getFlowInstanceName());
        return ResponseEntity.ok(new FlowInstanceCreateDTO(flowInstance.getPid()));
    }

    @PostMapping("/flow/process")
    @SysLog(operation = "流程处理")
    @ApiOperation(value = "流程处理", notes = "流程处理")
    @ApiOperationSort(14)
    public ResponseEntity<DoProcessDTO> doFlowProcess(@Validated @RequestBody DoProcessParam param) {
        String userId = SecurityUtils.getUser().getUserId();
        flowInstanceService.doFlowProcess(param.getFlowInstanceId(), param.getFlowProcessId(), userId, param.getSuggestion());
        return ResponseEntity.ok(new DoProcessDTO(1, "操作成功"));
    }


}
